package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.example.entity.Doctor;
import com.example.entity.User;
import com.example.entity.req.Page;
import com.example.entity.res.DynamicRes;
import com.example.mapper.UserMapper;
import com.example.service.DoctorService;
import com.example.service.DynamicService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    DynamicService dynamicService;

    @Autowired
    UserMapper userMapper;

    @Autowired
    DoctorService doctorService;

    /**
     * 获取自己所有动态
     *
     * @return
     */
    @GetMapping("/listAllDynamic")
    public List<DynamicRes> listAllDynamic() {
        return dynamicService.listAllDynamic();
    }

    /**
     * 分页获取User信息
     *
     * @param page
     * @return
     */
    @PostMapping("/pageUser")
    public PageInfo<User> pageUser(@RequestBody Page page) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<User> userInfoList = new LambdaQueryChainWrapper<User>(userMapper).list();
        PageInfo<User> pageInfo = new PageInfo<>(userInfoList);
        return pageInfo;
    }

    /**
     * 封禁
     *
     * @param id
     */
    @PutMapping("/ban/{id}")
    public void Ban(@PathVariable Integer id) {
        User user = new User();
        User user1 = userMapper.selectById(id);
        if (user1.getStatus() == 1) {
            user.setId(id);
            user.setStatus(2);
            userMapper.updateByPrimaryKeySelective(user);
        } else {
            user.setId(id);
            user.setStatus(1);
            userMapper.updateByPrimaryKeySelective(user);
        }
    }

    /**
     * 分页获取医生申请信息
     *
     * @param page
     * @return
     */
    @PostMapping("/pageDoctor")
    public PageInfo<Doctor> pageDoctor(@RequestBody Page page) {
        return doctorService.pageDoctor(page);
    }

    /**
     * 设置医生
     */
    @Transactional(rollbackFor = Exception.class)
    @PutMapping("/setDoctor")
    public void setDoctor(@RequestParam String phone, @RequestParam Integer id) {
        User user = new User();
        user.setPhone(phone);
        user.setRoleId(3);
        userMapper.updateByPhone(user);
        doctorService.del(id);
    }

}
